package com.gopay.common.domain.bank.ccb.cbrmb.res;

import java.io.Serializable;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;

import com.gopay.common.domain.bank.ccb.cbrmb.ByteData;
import com.gopay.common.util.cbrmb.MsgUtils;

/**
 * 
 * @ClassName: CbRmbResMsg
 * @Description: 通知返回报文数据
 * @author gd.cui
 * @date 2016年6月29日 上午11:29:49 ww.gopay.com.cn Inc.All rights reserved.
 */
public class CbRmbResponseMsg implements ByteData, Serializable {

    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = -6625389486054830718L;

    private static Logger logger = Logger.getLogger(CbRmbResponseMsg.class);

    private static final int MSG_RES_LENGTH = 129;
    /**
     * 通知方id 取请求报文sender_id域，原文返回
     */
    private String senderId;
    /**
     * 交易类型 01-来盘，02-回盘
     */
    private String tranType;
    /**
     * 交易流水 唯一标识每一笔通知，yyyyMMddHHmmss+3位序号，取请求报文流水原文返回
     */
    private String tranSn;

    /**
     * 返回码 000000:请求接收成功 900001:文件名重复 900002:文件不存在 900003：文件HASH值校验失败
     * 900004:其它错误 900005 文件获取失败
     * 
     */
    private String retCode;
    /**
     * 返回说明
     */
    private String retMsg;

    private byte[] senderIdByte = new byte[4];

    private byte[] tranTypeByte = new byte[2];

    private byte[] tranSnByte = new byte[17];

    private byte[] retCodeByte = new byte[6];

    private byte[] retMsgByte = new byte[100];

    public CbRmbResponseMsg() {
    }

    public CbRmbResponseMsg(byte[] source) {
        initPropertiesByBytes(source);
    }

    @Override
    public byte[] getByteData() {
        covertPropertiesToBytes();
        return MsgUtils.mergeBytes(this.senderIdByte, this.tranTypeByte, this.tranSnByte, this.retCodeByte,
                this.retMsgByte);
    }

    private void covertPropertiesToBytes() {
        byte[] senderIdSource = this.senderId.getBytes();
        int senderIdLength = senderIdSource.length;
        MsgUtils.copyBytes(senderIdSource, this.senderIdByte, 1, senderIdLength, 1);

        byte[] tranTypeSource = this.tranType.getBytes();
        int tranTypeLength = tranTypeSource.length;
        MsgUtils.copyBytes(tranTypeSource, this.tranTypeByte, 1, tranTypeLength, 1);

        byte[] tranSnSource = this.tranSn.getBytes();
        int tranSnLength = tranSnSource.length;
        MsgUtils.copyBytes(tranSnSource, this.tranSnByte, 1, tranSnLength, 1);

        byte[] retCodeSource = this.retCode.getBytes();
        int retCodeLength = retCodeSource.length;
        MsgUtils.copyBytes(retCodeSource, this.retCodeByte, 1, retCodeLength, 1);

        byte[] retMsgSource = this.retMsg.getBytes(Charset.forName("GBK"));
        int retMsgLength = retMsgSource.length;
        MsgUtils.copyBytes(retMsgSource, this.retMsgByte, 1, retMsgLength, 1);

    }

    @Override
    public void initPropertiesByBytes(byte[] source) {
        if (source != null && source.length >= MSG_RES_LENGTH) {
            MsgUtils.copyBytes(source, this.senderIdByte, 1, 4, 1);
            MsgUtils.copyBytes(source, this.tranTypeByte, 5, 2, 1);
            MsgUtils.copyBytes(source, this.tranSnByte, 7, 17, 1);
            MsgUtils.copyBytes(source, this.retCodeByte, 24, 6, 1);
            MsgUtils.copyBytes(source, this.retMsgByte, 30, 100, 1);

            this.senderId = new String(this.senderIdByte);
            this.tranType = new String(this.tranTypeByte);
            this.tranSn = new String(this.tranSnByte);
            this.retCode = new String(this.retCodeByte);
            this.retMsg = new String(this.retMsgByte, Charset.forName("GBK"));
        } else {
            logger.error("init ResMsg from bytes error: the bytes not enough");
        }
    }

    public String getSenderId() {
        return senderId;
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }

    public String getTranType() {
        return tranType;
    }

    public void setTranType(String tranType) {
        this.tranType = tranType;
    }

    public String getTranSn() {
        return tranSn;
    }

    public void setTranSn(String tranSn) {
        this.tranSn = tranSn;
    }

    public String getRetCode() {
        return retCode;
    }

    public void setRetCode(String retCode) {
        this.retCode = retCode;
    }

    public String getRetMsg() {
        return retMsg;
    }

    public void setRetMsg(String retMsg) {
        this.retMsg = retMsg;
    }

    @Override
    public String toString() {
        return " ResMsg [senderId=" + this.senderId + ",tranType=" + this.tranType + ",tranSn=" + this.tranSn
                + ",retCode=" + this.retCode + ",retMsg=" + this.retMsg + "]";
    }

}
